{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ecb1771",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "# ^^^ pyforest auto-imports - don't write above this line\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04a4b0ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm.auto import tqdm\n",
    "from copy import deepcopy\n",
    "\n",
    "from torch.nn.parallel import DistributedDataParallel as DDP, DataParallel as DP\n",
    "\n",
    "from dataset import CPSC2021\n",
    "from cfg import TrainCfg, ModelCfg\n",
    "from trainer import CPSC2021Trainer, _set_task, _MODEL_MAP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2aaaba92",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d80cd943",
   "metadata": {},
   "outputs": [],
   "source": [
    "TrainCfg.db_dir = \"/home/wenhao/Jupyter/wenhao/data/CPSC2021/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc78f3b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f953dc42",
   "metadata": {},
   "outputs": [],
   "source": [
    "CPSC2021.__DEBUG__ = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a64159d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "task = \"qrs_detection\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7504f876",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_train = CPSC2021(TrainCfg, training=True, task=task, lazy=False)\n",
    "ds_val = CPSC2021(TrainCfg, training=False, task=task, lazy=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94b9335e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed5a4415",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63e68e56",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88ac5516",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_config = deepcopy(TrainCfg)\n",
    "\n",
    "train_config.main.loss = \"AsymmetricLoss\" # \"MaskedBCEWithLogitsLoss\"\n",
    "train_config.main.loss_kw = ED(gamma_pos=0, gamma_neg=1, implementation=\"deep-psp\")\n",
    "\n",
    "# train_config.batch_size = 4\n",
    "_set_task(task, train_config)\n",
    "model_config = deepcopy(ModelCfg[task])\n",
    "\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffa2af59",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_cls = _MODEL_MAP[train_config[task].model_name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8698fc06",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = model_cls(config=model_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ba66a14",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.module_size_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cde643cf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "if task != \"rr_lstm\" and torch.cuda.device_count() > 1:\n",
    "    model = DP(model)\n",
    "    # model = DDP(model)\n",
    "model.to(device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29c0862f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "trainer = CPSC2021Trainer(\n",
    "    model=model,\n",
    "    model_config=model_config,\n",
    "    train_config=train_config,\n",
    "    device=device,\n",
    "    lazy=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fb4eeb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer._setup_dataloaders(ds_train, ds_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92b94555",
   "metadata": {},
   "outputs": [],
   "source": [
    "trainer.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ec111e1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c4efd06",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
